/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details:
 * http://www.gnu.org/copyleft/lesser.html#TOC3
 */
package com.simplemonkey.widgets;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.jme.image.Texture;

public class SimpleButton extends TextWidget {

		private List<SimpleButtonListener> listeners = new CopyOnWriteArrayList<SimpleButtonListener>();
		
	
		private Texture activeTexture;
		private Texture inactiveTexture;
		private Texture hoverTexture;
		
		public SimpleButton(String name,Texture activeTexture, Texture inactiveTexture,Texture hoverTexture) {
			super(name);
			this.activeTexture = activeTexture;
			this.inactiveTexture = inactiveTexture;
			this.hoverTexture = hoverTexture;
		
			
			setTexture(inactiveTexture, 0);
		}
		public SimpleButton(Texture activeTexture, Texture inactiveTexture,Texture hoverTexture) 
		{
			this("SimpleButton",activeTexture,inactiveTexture,hoverTexture);
		}
		public SimpleButton(Texture activeTexture, Texture inactiveTexture) 
		{
			this("SimpleButton",activeTexture,inactiveTexture,inactiveTexture);
		}
		public SimpleButton(String name,Texture activeTexture, Texture inactiveTexture) 
		{
			this(name,activeTexture,inactiveTexture,inactiveTexture);
		}
		public SimpleButton(String text) 
		{
			super(text);
			
		}
		public void addButtonListener(SimpleButtonListener listener)
		{
			listeners.add(listener);
		}
		
		public void removeToggleListener(ToggleListener listener)
		{
			listeners.remove(listener);
		}
		
		public void clearListeners()
		{
			listeners.clear();
		}
		
		public void setAllButtonTextues(Texture texture)
		{
			hoverTexture = texture;
			inactiveTexture = texture;
			activeTexture =texture;
			setTexture(inactiveTexture, 0);
		}
		
		@Override
		public boolean mousePress(boolean pressed, int button, float x, float y) {
			super.mousePress(pressed, button, x, y);
			
			if(pressed && button == 0)
			{
				if(activeTexture!=null)
					setTexture(activeTexture, 0);
				for(SimpleButtonListener listener:listeners)
				{
					listener.buttonPressed();
				}
			}else if(button == 0)
			{		
				if(hoverTexture!=null && inactiveTexture!=null)
					setTexture(isMouseTouching()?hoverTexture:inactiveTexture, 0);
			}
			return ! isPermeable();
		}

		@Override
		public void mouseOff() {
			if(inactiveTexture!=null)
				setTexture(inactiveTexture, 0);
			super.mouseOff();
		}

		@Override
		public boolean mouseMove(float x, float y) {
			if(hoverTexture!=null)
			{
				if(getTexture(0)!= activeTexture)
					setTexture(hoverTexture,0);
			}
			super.mouseMove(x, y);
			return ! isPermeable();
		}

		


	}
